package com.example.contestsystem.Controller;

import com.example.contestsystem.Entity.Student;
import com.example.contestsystem.Entity.Team;
import com.example.contestsystem.R.R;
import com.example.contestsystem.Service.ContestService;
import com.example.contestsystem.Service.JoinTeamService;
import com.example.contestsystem.Service.TeamService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/team")
public class TeamController {
    final TeamService teamService;
    final JoinTeamService joinTeamService;
    final ContestService contestService;

    public TeamController(TeamService teamService, JoinTeamService joinTeamService,
                          ContestService contestService) {
        this.teamService = teamService;
        this.joinTeamService = joinTeamService;
        this.contestService = contestService;
    }


    /**
     * 此方法实现创建队伍
     */
    @PostMapping("/createTeam")
    public String creatTeam(@ModelAttribute Team team, HttpServletRequest httpServletRequest) {
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        // 检查是否参加竞赛
        if(loginStu != null && this.contestService.checkParticipated(team.getContestId(), loginStu.getStuAccount())){
            team.setCaptainAccount(loginStu.getStuAccount());
            if (this.teamService.createTeam(team)) {
                return "redirect:/contest/detail?contestId="+team.getContestId();
            }
        }
        return null;
    }

    /**
     * 此方法实现队长删除自己的队伍
     */
    @GetMapping("/deleteTeam")
    @ResponseBody
    public R deleteTeam(@Param("teamId") String teamId, HttpServletRequest httpServletRequest) {
        Student loginStu = (Student) httpServletRequest.getSession().getAttribute("loginStu");
        // 检查登录用户是不是目标队伍的队长
        if (loginStu != null && this.teamService.verifyCaptain(teamId, loginStu.getStuAccount())) {
            if (this.teamService.deleteTeam(teamId))
                return R.ok(200, "SUCCESS");
        }
        return R.error(-1, "FAIL");
    }

    /**
     * 此方法获取前端参数contestId和teamId，从session获得stuAccount
     * 创建JoinTeam对象存到表JoinTeam
     */
    @PostMapping("/joinTeam")
    public String joinTeam(@ModelAttribute Team team, HttpServletRequest httpServletRequest, Model model) {
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        Team desTeam = this.teamService.findByTeamId(team.getTeamId());
        if(loginStu != null && this.teamService.joinTeam(team.getTeamId(), loginStu.getStuAccount())){
            model.addAttribute("team", desTeam);
            return "redirect:/team/detail?contestId="+desTeam.getContestId();
        }
        return "/stu/login";
    }


    /**
     * 此方法实现队员退出目标队伍，队长只能
     */
    @GetMapping("/exitTeam")
    @ResponseBody
    public R exitTeam(@Param("teamId") String teamId, HttpServletRequest httpServletRequest) {
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        if (loginStu!=null && this.joinTeamService.exit(teamId, loginStu.getStuAccount())) {
            return R.ok(200, "SUCCESS");
        }
        return R.error(-1, "FAIL");
    }


    @GetMapping("/isJoinedTeam")
    @ResponseBody
    public R isJoinedTeam(@Param("contestId") String contestId, HttpServletRequest httpServletRequest){
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        if(loginStu != null && this.joinTeamService.checkJoined(contestId, loginStu.getStuAccount())){
            return R.ok(200, "在队伍中");
        }
        return R.error(-1, "不在队伍中");
    }

    @GetMapping("/detail")
    public String detail(@Param("teamId") String contestId, HttpServletRequest httpServletRequest,
                         Model model){
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        Team desTeam = this.teamService.findByStuAccountAndContestId(loginStu.getStuAccount(), contestId);
        if(desTeam != null){
            Page<Student> students = this.teamService.findByTeamId(desTeam.getTeamId(), 0, 20);
            model.addAttribute("team", desTeam);
            model.addAttribute("students", students);
        }
        return "stu/team_detail";
    }

    @GetMapping("/isCaptain")
    @ResponseBody
    public R isCaptain(HttpServletRequest httpServletRequest, @Param("teamId") String teamId){
        Student loginStu = (Student)httpServletRequest.getSession().getAttribute("loginStu");
        if(this.teamService.verifyCaptain(teamId, loginStu.getStuAccount())){
            return R.ok(200, "yes");
        }
        return R.error(-1, "no");
    }
}
